/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gtd;

import java.util.*;
import javax.swing.tree.*;

/**
 *
 * @author d
 */
public class TreeHelper {

    /** Adds a new node to the given node */
    public static DefaultMutableTreeNode addNode(DefaultMutableTreeNode parent, Object child) {
        DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child);
        parent.add(childNode);
        return childNode;
    }
    
    /** Determines if the given node contains the given child */
    public static boolean nodeContains(DefaultMutableTreeNode node, Object child) {
        for (Enumeration e = node.children(); e.hasMoreElements();) {
            DefaultMutableTreeNode childNode = (DefaultMutableTreeNode)e.nextElement();
            if (childNode.getUserObject() == child) {
                return true;
            }
        }
        return false;
    }
    
    /** Gets the given child from the given node, or null if not found */
    public static DefaultMutableTreeNode getChild(DefaultMutableTreeNode node, Object child) {
        for (Enumeration e = node.children(); e.hasMoreElements();) {
            DefaultMutableTreeNode childNode = (DefaultMutableTreeNode)e.nextElement();
            if (childNode.getUserObject().equals(child)) {
                return childNode;
            }
        }
        return null;
    }
}
